package com.longrise.co.ui.carlife.model;

import com.google.gson.reflect.TypeToken;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.api.ApiService;
import com.longrise.co.ui.carlife.contract.CarLifeContract;
import com.ly.common.basebean.BaseResponse;
import com.ly.common.basebean.KckpBaseResponse;
import com.ly.common.baserx.RxResponseFuncForCar;
import com.ly.common.baserx.RxResponseFuncForKcKp;
import com.ly.common.commonutils.JsonUtils;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by luoyang on 2017-6-23.
 */

public class CarLifeModel implements CarLifeContract.Model {

    @Override
    public Observable<NewsBean> carLifeNewsRequest(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<NewsBean>>(){}.getType(), bean, ApiConstants.APPSEARCHCARNEWSLIST)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<NewsBean>());
    }

    @Override
    public Observable<IllegalInquiriesBean> illegalInquiriesRequest(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<IllegalInquiriesBean>>(){}.getType(), bean, ApiConstants.LCIPSEARCHPECCANCY)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<IllegalInquiriesBean>());
    }

    @Override
    public Observable<YearCheckBean> yearCheckRequest(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<YearCheckBean>>(){}.getType(), bean, ApiConstants.LCIPCARINSPECTION)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<YearCheckBean>());
    }

    @Override
    public Observable<String> kckpUrlRequest(EntityBean bean) {
        return ApiService.getInstance().kckpRequest(new TypeToken<KckpBaseResponse<String>>(){}.getType(), bean, ApiConstants.GETHNKCKPURL)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForKcKp<String>());
    }

    @Override
    public Observable<String> kckpUploadPhotoRequest(EntityBean bean) {
        return ApiService.getInstance().kckpUploadPhotoRequest( bean, ApiConstants.UPLOADIMAGEINFO)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        KcKpUploadPhotoBean bean = (KcKpUploadPhotoBean)JsonUtils.fromJson(s, KcKpUploadPhotoBean.class);
                        if(bean != null && "1".equals(bean.getRestate())) {
                            return s;
                        } else {
                            throw new IllegalStateException("上传图片失败");
                        }
                    }
                });
    }
}
